package com.example.governmentcrawler.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.governmentcrawler.constant.EmailConst;
import com.example.governmentcrawler.constant.KeyWordsConst;
import com.example.governmentcrawler.constant.UrlConst;
import com.example.governmentcrawler.service.EmailService;
import com.example.governmentcrawler.service.GetDetailUrlSets;
import com.example.governmentcrawler.utils.GetDetailId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @projectName: GovernmentCrawler
 * @package: com.example.governmentcrawler.controller
 * @className: MyTestController
 * @author: xxXPCSC
 * @description: TODO
 * @date: 2024/2/26 17:21
 * @version: 1.0
 **/
@RestController
public class MyTestController {
    @Autowired
    private GetDetailUrlSets getDetailUrlSets;
    @Autowired
    private EmailService emailService;

    @GetMapping("/getId")
    public Map<String,Map> getDetailTestId(){
        return getDetailUrlSets.getDetailUrlSetsForGov();
    }

    @GetMapping("/getFilterId")
    public Map<String,Map> getDetailId(){
        return getDetailUrlSets.getDetailUrlSetsForGovFilter();
    }


    @GetMapping("/getOnline")
    public Map<String,String> getOnlineId(){
        return getDetailUrlSets.getDetailUrlSetsForOnLineMarket();
    }


    @GetMapping("/sendEmail")
    @Scheduled(cron = "0 0 9 * * ?")
    public String sendEmail(){
        //1097405369
        Map<String, Map> allUrls = getDetailUrlSets.getFilterUrls();
        List<String> failEmail = new ArrayList<>();
        for (int i=0;i< EmailConst.Emails.length;i++){
            if(emailService.sendDay(EmailConst.Emails[i], allUrls))
                //上一封邮件发送成功继续下一封邮件的发送
                continue;
            else{
                failEmail.add(EmailConst.Emails[i]);
                continue;
            }
        }
        emailService.sendDay("2642385521@qq.com",allUrls);
        emailService.send("2642385521@qq.com",failEmail);
        if(failEmail.isEmpty())
            return "发送成功";
        else
            return "发送失败";
    }

    @GetMapping("/sendEmailEz")
    @Scheduled(cron = "0 0 23 * * ?")
    public String sendEmailTest(){
        Map<String, Map> allUrls = getDetailUrlSets.getFilterUrls();
        //1097405369
        if(emailService.sendDay("2642385521@qq.com",allUrls))
            return "发送成功";
        return "发送失败";
    }


    @GetMapping("/getAllUrls")
    public Map<String,Map> getAllUrls(){
        return getDetailUrlSets.getAllUrls();
    }
}
